/**
 * Created by arron_Li on 18/4/11. // 基于axios改造的全局公共异步请求
 */
import axios from 'axios'
let qs = require('qs')
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8' // post 请求增加请求头
// axios.defaults.withCredentials = true // 设置带cookie参数

// let commonUrl = 'http://10.0.92.179:3000/api/employee'
// let commonUrl = 'http://localhost:3000/api/employee'
// let commonUrl = 'http://www.personal-card.com/employee'
let commonUrl = 'http://cardtest.adyun.com/employee'

axios.interceptors.response.use( // 利用axios拦截器鉴权和写公共的错误分支
	response => {
		return response
	},
	error => {
		console.log('服务器异常！')
	}
)

export default {
	fetchGet(url, params = {}) {
		return new Promise((resolve, reject) => {
			axios.get(commonUrl + url, {
				params
			}).then(res => {
				resolve(res.data)
			}).catch(error => {
				reject(error)
			})
		})
	},
	fetchPost(url, params = {}) {
		return new Promise((resolve, reject) => {
			axios.post(commonUrl + url, qs.stringify(params)).then(res => {
				resolve(res.data)
			}).catch(error => {
				reject(error)
			})
		})
	},
	fetchImg(url, params = {}) {
		return new Promise((resolve, reject) => {
			axios.post(commonUrl + url, params).then(res => {
				resolve(res.data)
			}).catch(error => {
				reject(error)
			})
		})
	},
	fetchAll(params) {
		return new Promise((resolve, reject) => {
			axios.all([...params]).then(axios.spread((...results) => {
				resolve(results)
			})).catch(error => {
				reject(error)
			})
		})
	}
}